﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using TomNet.IMSC.Core.Contracts.Finance;
using TomNet.IMSC.Core.Contracts.Organization;
using TomNet.IMSC.Model.DbModels.Finance;
using TomNet.IMSC.Web.Areas.Finance.Models;
using TomNet.Web.Mvc;
using TomNet.Web.Mvc.Security;
using TomNet.Web.Mvc.UI;

namespace TomNet.IMSC.Web.Areas.Finance.Controllers
{
    [Description("管理-金融总流水")]
    public class RunningController : BaseController
    {
        public IFinanceRunningsContract Contract { get; set; }
        public IStaffContract StaffContract { get; set; }
        public IStaffRolesContract StaffRolesContract { set; get; }

        [Description("管理-金融总流水")]
        public ActionResult Index()
        {
            return View();
        }


        [Description("管理-金融总流水")]
        [HttpPost, AjaxOnly, NotCollected]
        public ActionResult IndexAsync(string id, int pageIndex, int pageSize, string search = "")//, int depid
        {
            var query = Contract.TrackEntities;// && x.DepartmentId == depid);

            //数据总数
            var total = query.Count();

            //默认给一个排序的字段
            query = query.OrderBy(m => m.Id);

            //分页(假如total == 0，则取消分页查询，提高性能)
            query = total > 0 ? query.Skip((pageIndex - 1) * pageSize).Take(pageSize)
                : Enumerable.Empty<FinanceRunnings>().AsQueryable();

            GridData<FinanceRunnings> gridData = new GridData<FinanceRunnings>() { Total = total, Rows = query.ToList() };

            //此处采用重写后的jsonResult
            return JsonEx(gridData, JsonRequestBehavior.AllowGet, "yyyy-MM-dd HH:mm:ss");
        }

        #region 修改流水相关操作
        [Description("管理-金融项属性")]
        //[GlobalAuthorization]
        public ActionResult Edit(int id, int type)
        {
            var entity = Contract.GetByKey(id);
            //后台容错，有异常数据直接抛出。框架会自动跳转到错误页面。
            if (entity == null) throw new Exception("未找到该实体");

            entity.Progress = type;//临时借用
            ViewData["entity"] = entity;
            return View();
        }

        [Description("管理-金融项属性")]
        [HttpPost, AjaxOnly, NotCollected]
        public ActionResult EditAsyncBy(FinanceRunnings entity)
        {
            var result = new AjaxResult();
            try
            {

                var obj = Contract.GetByKey(entity.Id);
                if (entity.Progress == 2)//修改 转件明细
                {
                    obj.TransferDetailed = entity.TransferDetailed;
                    obj.TransferDetailedRemark = entity.TransferDetailedRemark;
                }
                else if (entity.Progress == 9)//修改客户姓名
                {
                    obj.CustomerName = entity.CustomerName;
                }
                else if (entity.Progress == 1)//修改备注
                {
                    obj.Remark = entity.Remark;
                }
                else if (entity.Progress == 10)
                {
                    obj.CustomerPhone = entity.CustomerPhone;
                }

                var count = Contract.Update(obj);
                if (count > 0)
                {
                    result.Type = AjaxResultType.Success;
                    result.Content = "修改成功";
                }
                else
                {
                    result.Type = AjaxResultType.Error;
                    result.Content = "修改失败";
                }

            }
            catch
            {
                result.Type = AjaxResultType.Error;
                result.Content = "异常操作";
            }
            return JsonEx(result);
        }
        #endregion


        [Description("管理-金融代办员-列表")]
        [HttpPost, AjaxOnly]
        public ActionResult GetUserAsync()
        {
            var result = new AjaxResult();
            try
            {
                List<BaseName> oaList = new List<BaseName>();
                var doList = StaffRolesContract.Entities.Where(x => x.RoleID == 22).Select(x => x.StaffID).ToList();//金融办件员

                if (doList.Count > 0)
                {
                    var list = StaffContract.Entities.Where(x => doList.Contains(x.Id)).Select(x => new BaseName() { Name = x.Name, Id = x.Id });
                    if (list.Count() > 0)
                    {
                        result.Type = AjaxResultType.Success;
                        result.Data = list;
                        result.Content = "成功";
                    }
                    else
                    {
                        result.Type = AjaxResultType.Error;
                        result.Content = "失败";
                    }
                }
                else
                {
                    result.Type = AjaxResultType.Success;
                    result.Data = oaList;
                    result.Content = "成功";
                }

            }
            catch (Exception ex)
            {
                result.Type = AjaxResultType.Error;
                result.Content = "异常操作";
            }
            return JsonEx(result);
        }

        [Description("管理-获取金融流水")]
        [HttpPost, AjaxOnly, NotCollected]
        public ActionResult EditModel(int id)
        {
            var result = new AjaxResult();
            try
            {
                var query = Contract.GetByKey(id);
                if (query == null)
                {
                    result.Type = AjaxResultType.Error;
                    result.Content = "获取失败";
                }
                else
                {
                    result.Type = AjaxResultType.Success;
                    result.Content = "成功";
                    result.Data = query;
                }
            }
            catch
            {
                result.Type = AjaxResultType.Error;
                result.Content = "异常操作";
            }
            return JsonEx(result);
        }
    }
    public enum BusinessLoanProgress
    {
        //未签 = 0,
        报备审核 = 1,
        资料收集 = 2,
        //分配办件人 = 3,
        //设置面签时间 = 4,
        面签 = 3,
        审批 = 4,
        放款 = 5,
    }
    public enum TransferProgress
    {
        //未签 = 0,
        报备审核 = 1,
        资料收集 = 2,
        //分配办件人 = 3,
        //设置查档时间 = 4,
        报税查档 = 3,
        过户 = 4,
        下证 = 5,
    }
    public enum PAProgress
    {
        //未签 = 0,
        报备审核 = 1,
        资料收集 = 2,
        面签 = 3,
        抵押 = 4,
        放款 = 5,
    }


    public enum ProgressStr
    {
        //未签 = 0,
        报备 = 1,
        资料收集 = 2,
        面签 = 3,
        审批 = 4,
        放款 = 5,
    }
    public enum Progress_State
    {
        //0未分配  1进行中  2已完成  3异常
        //未签 = 0,
        待分配 = 0,
        进行中 = 1,
        已完成 = 2,
        已终止 = 3,//终止
    }
    /// <summary>
    /// 1正常  2异常   3完成
    /// </summary>
    public enum FinanceRunning_State
    {
        正常 = 1,
        异常 = 2,
        完成 = 3,
    }
    public enum Option_State
    {
        中断 = 1,
        完成 = 2,
        终止 = 3
    }

    public enum LoanType
    {
        商业贷款 = 3,//id
        公积金贷款 = 4,//parentId
        金融过户 = 5,//parentId

    }


}